#include "book.h"
#include"QDebug"
Book::Book()
{

}

Book::Book(const Book &bok)
{
    this->book_number = bok.book_number;
    this->book_ISBN = bok.book_ISBN;
    this->book_name = bok.book_name;
    this->book_writer = bok.book_writer;
    this->book_num = bok.book_num;
    this->book_price = bok.book_price;
    this->book_PublishTime = bok.book_PublishTime;
    this->book_PublishUnit = bok.book_PublishUnit;
    this->book_array.clear();
    for(int i = 0; i < bok.book_array.size();i++)
    {
        Vectorbook cnt(bok.book_array[i].book_number,bok.book_array[i].book_state,bok.book_array[i].book_Remarks);
        this->book_array.push_back(cnt);
    }
}

Book_state stringTostate(QString str)
{
    int num = str.toInt();
    if(num == 1)
    {
        return Lending;//借出
    }
    else if(num == 2)
    {
        return Not_Lending;//未借出
    }
    else if(num == 3)
    {
        return Lost;//丢失
    }else if(num == 4)
    {
        return Destroyed;//毁坏
    }
    else if(num == 5)
    {
        return Damage;//破损
    }
}

Book::Book(QStringList data)
{
    this->book_number=data[0];
    this->book_ISBN=data[1];
    this->book_name=data[2];
    this->book_writer=data[3];
    this->book_price=data[4].toFloat();
    this->book_num=data[5].toUInt();
    this->book_PublishTime=QDate::fromString( data[6],"yyyy年MM月");
    this->book_PublishUnit=data[7];
//    qDebug()<<this->book_num;
    this->book_array.clear();
    int n = this->book_num*3+8;
    for(int i = 0; i < n; i+=3)
    {
        if(i+10 >= n) break;
//        qDebug()<<__LINE__<<"   "<<i<<endl;
        Vectorbook cnt(data[i+8],stringTostate(data[i+9]),data[i+10]);
        this->book_array.push_back(cnt);
    }
}

Book& Book::operator=(const Book &bok)
{
    this->book_number = bok.book_number;
    this->book_ISBN = bok.book_ISBN;
    this->book_name = bok.book_name;
    this->book_writer = bok.book_writer;
    this->book_num = bok.book_num;
    this->book_price = bok.book_price;
    this->book_PublishTime = bok.book_PublishTime;
    this->book_PublishUnit = bok.book_PublishUnit;
    this->book_array.clear();
    for(int i = 0; i < bok.book_array.size();i++)
    {
        Vectorbook cnt(bok.book_array[i].book_number,bok.book_array[i].book_state,bok.book_array[i].book_Remarks);
        this->book_array.push_back(cnt);
    }
    return *this;
}

Book::Book(QString number, QString ISBN,QString name, QString writer, float price, size_t num, QDate PublishTime, QString PublishUnit)
{
    this->book_number=number;
    this->book_ISBN=ISBN;
    this->book_name = name;
    this->book_writer=writer;
    this->book_price=price;
    this->book_num=num;
    this->book_PublishTime=PublishTime;
    this->book_PublishUnit=PublishUnit;
    this->book_array=BuildBookNumber(number, num);
}

//Book::Book(QString number, QString ISBN, QString writer, float price, size_t num, QDate PublishTime, QString PublishUnit, QVector<Vectorbook> array)
//{
//    this->book_number=number;
//    this->book_ISBN=ISBN;
//    this->book_writer=writer;
//    this->book_price=price;
//    this->book_num=num;
//    this->book_PublishTime=PublishTime;
//    this->book_PublishUnit=PublishUnit;
//    this->book_array=array;
//}

QVector<Vectorbook> Book::BuildBookNumber(QString number, size_t num)
{
    QVector<Vectorbook> ret;
    for(size_t i = 1; i <= num; i++)
    {
        Vectorbook cnt(number + QString::number(i).rightJustified(5,'0'));
        ret.push_back(cnt);
    }

    return ret;
}

QStringList Book::getBook()
{
    QStringList ret;
    ret.append(this->book_number);
    ret.append(this->book_ISBN);
    ret.append(this->book_name);
    ret.append(this->book_writer);
    ret.append(QString::number(this->book_price,'f',2));
    ret.append(QString::number(this->book_num));
    ret.append(this->book_PublishTime.toString("yyyy年MM月"));
    ret.append(this->book_PublishUnit);
    for(int i = 0; i < this->book_array.size(); i++)
    {
        ret.append(this->book_array[i].book_number);
        ret.append(QString::number(this->book_array[i].book_state));
        ret.append(this->book_array[i].book_Remarks);
    }
    return ret;
}

Vectorbook::Vectorbook()
{
}

Vectorbook::Vectorbook(const Vectorbook &book)
{
    this->book_state = book.book_state;
    this->book_number = book.book_number;
    this->book_Remarks = book.book_Remarks;
}

Vectorbook &Vectorbook::operator=(const Vectorbook &book)
{
    this->book_state = book.book_state;
    this->book_number = book.book_number;
    this->book_Remarks = book.book_Remarks;
}


Vectorbook::Vectorbook(QString number)
{
    this->book_number=number;
    this->book_state=Not_Lending;
    this->book_Remarks="";
}

Vectorbook::Vectorbook(QString number, Book_state state, QString Reamarks)
{
    this->book_number = number;
    this->book_state = state;
    this->book_Remarks = Reamarks;
}


